package design.graduation.novelsystem.controller;

import design.graduation.novelsystem.model.domian.SystemNotice;
import design.graduation.novelsystem.model.dto.ResultObj;
import design.graduation.novelsystem.model.vo.SystemNoticeVo;
import design.graduation.novelsystem.service.SystemNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author: 洪少尉
 * @Date: 2020/4/15 16:14
 * @Description: 系统公告管理
 */
@RestController
@RequestMapping("systemNotice")
public class SystemNoticeController {
	@Autowired
	private SystemNoticeService systemNoticeService;
	/**
	 * 查询
	 */
	@GetMapping("loadAllSystemNotice")
	@RequiresPermissions("notice:show")
	public Object loadAllSystemNotice(SystemNoticeVo systemNoticeVo){
		return this.systemNoticeService.queryAllSystemNotice(systemNoticeVo);
	}

	/**
	 * 添加
	 */
	@PostMapping("addSystemNotice")
	@RequiresPermissions("notice:add")
	public ResultObj addSystemNotice(SystemNotice systemNotice){
		if (this.systemNoticeService.saveSystemNotice(systemNotice)){
			return ResultObj.ADD_SUCCESS;
		}
		return ResultObj.ADD_ERROR;
	}

	/**
	 * 修改
	 */
	@PostMapping("updateSystemNotice")
	@RequiresPermissions("notice:update")
	public ResultObj updateSystemNotice(SystemNotice systemNotice){
		systemNotice.setEditTime(new Date());
		if (this.systemNoticeService.updateById(systemNotice)){
			return ResultObj.UPDATE_SUCCESS;
		}
		return ResultObj.UPDATE_ERROR;
	}

	/**
	 * 删除
	 */
	@PostMapping("deleteSystemNotice")
	@RequiresPermissions("notice:delete")
	public ResultObj deleteSystemNotice(Integer id){
		if(this.systemNoticeService.removeById(id)){
			return ResultObj.DELETE_SUCCESS;
		}
		return ResultObj.DELETE_ERROR;
	}

	/**
	 * 批量删除
	 */
	@PostMapping("batchDeleteSystemNotice")
	@RequiresPermissions("notice:delete")
	public ResultObj batchDeleteSystemNotice(Integer[] ids){
		List<Integer> idsList = new ArrayList<>(Arrays.asList(ids));
		if(this.systemNoticeService.removeByIds(idsList)){
			return ResultObj.DELETE_SUCCESS;
		}
		return ResultObj.DELETE_ERROR;
	}
}
